﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Web_Oto.Business;
using Web_Oto.Common;
using Web_Oto.Data;

namespace Web_Oto.Administer
{
    public partial class SEOPage : System.Web.UI.Page
    {
        private SeoBus _business;        

        protected void Page_Load(object sender, EventArgs e)
        {
            _business = new SeoBus();            
        }

        protected void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            grid.DataSource = _business.GetSeos();
        }

        protected void grid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            var gdItem = (e.Item as GridEditFormItem);
            var editableItem = ((GridEditableItem)e.Item);
            var values = new Hashtable();
            editableItem.ExtractValues(values);

            if (gdItem != null)
            {
                var txtTitle = ((RadTextBox)gdItem.FindControl("txtTitle"));
                var txtKeyword = ((RadTextBox)gdItem.FindControl("txtKeyword"));
                var txtDescription = ((RadTextBox)gdItem.FindControl("txtDescription"));

                var seo = new SEO
                {
                    Id = (int)editableItem.GetDataKeyValue("Id"),
                    Title = txtTitle.Text,
                    Keyword = txtKeyword.Text,
                    Description = txtDescription.Text
                };

                if (!CheckValidData(seo.Title, seo.Keyword, seo.Description))
                    return;

                var result = _business.Update(seo);
                if (result > 0)
                {
                    msgBox.InnerHtml = "<p><strong>Cập nhật thành công</strong></p>";
                    msgBox.Attributes["class"] = "msg msg-ok";
                }
                else
                {
                    msgBox.InnerHtml = "<p><strong>Cập nhật không thành công</strong></p>";
                    msgBox.Attributes["class"] = "msg msg-error";
                }
            }
        }

        private bool CheckValidData(string title, string keyword, string description)
        {
            var result = true;
            var fielName = string.Empty;
            if (string.IsNullOrWhiteSpace(title))
            {
                fielName = fielName + " tiêu đề, ";
                result = false;
            }

            if (string.IsNullOrWhiteSpace(keyword))
            {
                fielName = fielName + " keyword, ";
                result = false;
            }

            if (string.IsNullOrWhiteSpace(description))
            {
                fielName = fielName + " mô tả, ";
                result = false;
            }

            if (!result)
            {
                fielName = fielName.TrimEnd(new[] { ',', ' ' });
                msgBox.InnerHtml = "<p><strong>Vui lòng nhập " + fielName + "</strong></p>";
                msgBox.Attributes["class"] = "msg msg-error";
            }

            return result;
        }
    }
}